<script setup lang="ts">
import MdViewer from "../../../../components/md/MdViewer.vue";
import {FavorControllerService, QuestionVO, ThumbControllerService} from "../../../../../generated";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import {Document, EditPen, Monitor, Pointer, Setting, Share, Star, StarFilled, Timer} from "@element-plus/icons-vue";

const props = defineProps<{
  questionData: QuestionVO,
  isProgrammingQuestion: boolean,
  renderPart?: 'header' | 'main' | 'footer' | 'all'
}>();

const router = useRouter();

interface DifficultyType {
  type: string;
  value: string;
}

const difficultyMap: Record<number, DifficultyType> = {
  0: {type: 'success', value: '简单'},
  1: {type: 'warning', value: '中等'},
  2: {type: 'danger', value: '困难'}
};

// 跳转到更新页面
const toUpdate = () => {
  router.push("/update/question/" + props.questionData.id);
};

// 处理点赞
const handleThumb = async () => {
  try {
    const res = await ThumbControllerService.thumbQuestionUsingPost({
      questionId: String(props.questionData.id)
    });
    if (res.code === 0) {
      props.questionData.isThumb = !props.questionData.isThumb;
      if (props.questionData.isThumb) {
        props.questionData.thumbNum = (props.questionData.thumbNum || 0) + 1;
      } else {
        props.questionData.thumbNum = Math.max((props.questionData.thumbNum || 0) - 1, 0);
      }
    } else {
      ElMessage.error(res.message || '操作失败');
    }
  } catch (error) {
    ElMessage.error('操作失败');
  }
};

// 处理收藏
const handleFavor = async () => {
  try {
    const res = await FavorControllerService.favorQuestionUsingPost({
      questionId: String(props.questionData.id)
    });
    if (res.code === 0) {
      props.questionData.isFavour = !props.questionData.isFavour;
      if (props.questionData.isFavour) {
        props.questionData.favourNum = (props.questionData.favourNum || 0) + 1;
      } else {
        props.questionData.favourNum = Math.max((props.questionData.favourNum || 0) - 1, 0);
      }
    } else {
      ElMessage.error(res.message || '操作失败');
    }
  } catch (error) {
    ElMessage.error('操作失败');
  }
};

// 复制链接
const handleCopy = () => {
  let path = window.location.href;
  navigator.clipboard.writeText(path).then(() => {
    ElMessage.success('链接已复制到剪贴板');
  }).catch(() => {
    ElMessage.error('复制失败');
  });
};

// 处理反馈
const handleFeedback = () => {
  ElMessage.success('反馈成功');
};
</script>

<template>
  <div>
    <!-- 头部区域 -->
    <div v-if="renderPart === 'header' || renderPart === 'all'" class="question-header-container">
      <div class="question-title-container">
        <el-icon color="#007CFD" style="margin-right: 5px">
          <Document/>
        </el-icon>
        <div class="submission-title">题目描述</div>
      </div>

      <el-button
        type="primary"
        size="small"
        plain
        @click="toUpdate"
        class="edit-button-right"
      >
        <el-icon style="margin-right: 5px">
          <Setting/>
        </el-icon>
        修改
      </el-button>
    </div>

    <!-- 主体内容区域 -->
    <div v-if="renderPart === 'main' || renderPart === 'all'">
      <div class="question-content">
        <h2>{{ props.questionData.title }}</h2>
        <div style="margin-top: 20px; margin-bottom: 20px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center">
          <el-tag :type="difficultyMap[props.questionData.difficulty as number]?.type || 'info'" round>
            {{ difficultyMap[props.questionData.difficulty as number]?.value || '未知' }}
          </el-tag>
          <el-tag
              v-for="tag in props.questionData.tagList"
              :key="tag"
              effect="plain"
              round
          >
            {{ tag }}
          </el-tag>
        </div>
        <MdViewer :value="props.questionData.content || ''"></MdViewer>
      </div>
      <div v-if="isProgrammingQuestion" class="limits-container">
        <el-card class="limit-card" shadow="never">
          <template #header>
            <div class="limit-header">
              <el-icon>
                <Timer/>
              </el-icon>
              <span>时间限制</span>
            </div>
          </template>
          <div class="limit-value">{{ props.questionData.timeLimit }} ms</div>
        </el-card>

        <el-card class="limit-card" shadow="never">
          <template #header>
            <div class="limit-header">
              <el-icon>
                <Monitor/>
              </el-icon>
              <span>内存限制</span>
            </div>
          </template>
          <div class="limit-value">{{ props.questionData.memoryLimit }} MB</div>
        </el-card>
      </div>
      <el-divider/>
      <div class="question-stats">
        <div class="stat-item">
          <span class="stat-label">通过次数</span>
          <span class="stat-value">{{ props.questionData.acceptedNum }}</span>
        </div>
        <div class="divider"/>
        <div class="stat-item">
          <span class="stat-label">提交次数</span>
          <span class="stat-value">{{ props.questionData.submitNum }}</span>
        </div>
        <div class="divider"/>
        <div class="stat-item">
          <span class="stat-label">通过率</span>
          <span class="stat-value">
            {{ ((props.questionData.acceptedNum || 0) / (props.questionData.submitNum || 1) * 100).toFixed(2) }}%
          </span>
        </div>
      </div>
    </div>

    <!-- 底部区域 -->
    <div v-if="renderPart === 'footer' || renderPart === 'all'" class="question-footer">
      <el-button
          class="footer-thumb"
          :icon="Pointer"
          :type="props.questionData.isThumb ? 'primary' : 'default'"
          @click="handleThumb"
      >
        {{ props.questionData.thumbNum }}
      </el-button>
      <div class="footer-divider"/>
      <el-button
          class="footer-icon"
          :type="props.questionData.isFavour ? 'primary' : ''"
          text
          @click="handleFavor"
      >
        <template #icon>
          <el-icon :size="props.questionData.isFavour ? 18 : 14" :color="props.questionData.isFavour ? '#FFB800' : ''">
            <component :is="props.questionData.isFavour ? StarFilled : Star"/>
          </el-icon>
        </template>
      </el-button>
      <el-tooltip
          content="分享"
          placement="top"
          effect="light"
          :show-arrow="false"
      >
        <el-button class="footer-icon" text @click="handleCopy">
          <template #icon>
            <el-icon size="14">
              <Share/>
            </el-icon>
          </template>
        </el-button>
      </el-tooltip>
      <el-tooltip
          content="反馈"
          placement="top"
          effect="light"
          :show-arrow="false"
      >
        <el-button class="footer-icon" text @click="handleFeedback">
          <template #icon>
            <el-icon size="14">
              <EditPen/>
            </el-icon>
          </template>
        </el-button>
      </el-tooltip>
    </div>
  </div>
</template>

<style scoped>
.question-content {
  padding: 20px 16px;
}

.question-content h2 {
  margin: 0;
}

.limits-container {
  padding: 0 16px 20px;
  display: flex;
  gap: 16px;
}

.limit-card {
  flex: 1;
}

:deep(.el-card__header) {
  padding: 12px;
  border-bottom: 1px solid #e4e7ed;
}

:deep(.el-card__body) {
  padding: 12px;
}

.limit-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #606266;
}

.limit-value {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  text-align: center;
}

.question-stats {
  margin-top: 20px;
  margin-bottom: 20px;
  padding: 0 16px;
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-label {
  font-size: 12px;
  color: rgba(38, 38, 38, 0.75);
}

.stat-value {
  font-size: 14px;
  color: #262626;
  font-weight: 600;
}

.divider {
  width: 2px;
  height: 18px;
  background-color: #dcdfe6;
}

.question-footer {
  display: flex;
  align-items: center;
}

.footer-thumb {
  width: 73px;
  height: 28px;
  border-radius: .5rem;
}

.footer-thumb.el-button--primary {
  background-color: #007CFD;
  border-color: #007CFD;
  color: #fff;
}

.footer-divider {
  width: 1px;
  height: 16px;
  background-color: #dcdfe6;
  margin-left: 10px;
}

.footer-icon {
  width: 28px;
  margin-left: 4px;
  padding: 6px
}

.footer-icon.el-button--primary {
  color: #FFB800;
}

.submission-title {
  font-size: 14px;
  color: #1A1A1A;
  margin-right: 10px;
}

.edit-button-right {
  border-radius: 4px;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .limits-container {
    padding: 0 12px 16px;
    gap: 12px;
  }

  .limit-value {
    font-size: 14px;
  }
  
  .question-content {
    padding: 12px;
  }

  .question-stats {
    padding: 0 12px;
    gap: 12px;
  }
}

/* 添加新样式 */
.question-header-container {
  display: flex;
  justify-content: space-between;
  width: 100%;
  align-items: center;
}

.question-title-container {
  display: flex;
  align-items: center;
}
</style> 